# C:/Python27/TextResuming/word_weighting.py
# This is word_weighting file
#

from  __future__ import division
import math
import model.text as text
import operator

def getTF(word, bag_of_word):
	'''Return a TF value of a word from bag_of_word.

		bag_of_word is a list of word.'''
	return bag_of_word.count(word) / len(bag_of_word)

def getIDF(word, list_of_list_of_word):
	'''Return a IDF value of a word from list_of_list_of_word.
		
		With normalization.'''
	numOccur = 0
	for list_of_word in list_of_list_of_word:
		if (word in list_of_word):
			numOccur += 1
	return math.log(len(list_of_list_of_word) / (numOccur + 1))

def getTFIDF(word, bag_of_word, list_of_list_of_word):
	'''Return Tf.Idf value.'''
	return getTF(word, bag_of_word) * getIDF(word, list_of_list_of_word)

def getDictionary(news):
	'''Return a dictionary contain word and its TfIdf value.'''
	list_of_word = news.getSetOfWordContent()
	bag_of_word = news.getListOfWordContent()
	list_of_list_of_word = news.getListOfListOfWord()

	print 'list_of_word', list_of_word
	print 'bag_of_word', bag_of_word

	retval = dict()
	for word in list_of_word:
		TfIdf = getTFIDF(word, bag_of_word, list_of_list_of_word)
		retval[word] = TfIdf
	sorted_retval = sorted(retval.iteritems(), key=operator.itemgetter(1), reverse = True)
	return sorted_retval

if __name__ == '__main__':
	print 'hello'

	
